package cn.dgut.jy.soro.service.impl;

import cn.dgut.jy.soro.entity.CartItem;
import cn.dgut.jy.soro.entity.Order;
import cn.dgut.jy.soro.entity.OrderItem;
import cn.dgut.jy.soro.entity.Product;
import cn.dgut.jy.soro.repository.CartItemRepository;
import cn.dgut.jy.soro.repository.OrderItemRepository;
import cn.dgut.jy.soro.repository.OrderRepository;
import cn.dgut.jy.soro.repository.ProductRepository;
import cn.dgut.jy.soro.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    private OrderRepository orderRepository;
    private OrderItemRepository orderItemRepository;
    private CartItemRepository cartItemRepository;
    private ProductRepository productRepository;

    @Autowired
    public OrderServiceImpl(OrderRepository orderRepository,
                            OrderItemRepository orderItemRepository,
                            CartItemRepository cartItemRepository,
                            ProductRepository productRepository) {
        this.orderRepository = orderRepository;
        this.orderItemRepository = orderItemRepository;
        this.cartItemRepository = cartItemRepository;
        this.productRepository = productRepository;
    }

    @Override
    public Boolean createOrder(Integer userId, String address,String consignee, String mobile, String cartItemsStr) {
        Order order = new Order();
        order.setUserId(userId);
        order.setAddress(address);
        order.setConsignee(consignee);
        order.setMobile(mobile);
        order = orderRepository.saveAndFlush(order);
        Float totalPrice = 0F;

        String []strings = cartItemsStr.split(",");

        for (String s: strings
             ) {
            Integer cartItemId = Integer.parseInt(s);
            if(!cartItemRepository.findById(cartItemId).isPresent()) {
                return false;
            }

            CartItem cartItem = cartItemRepository.findById(cartItemId).get();
            OrderItem orderItem = new OrderItem();
            Integer productId = cartItem.getProductId();
            if(!productRepository.findById(productId).isPresent()) {
                return false;
            }
            Product product = productRepository.findById(productId).get();

            totalPrice += cartItem.getTotalPrice();
            orderItem.setAmount(cartItem.getAmount());
            orderItem.setOrderId(order.getId());
            orderItem.setProductId(productId);
            orderItem.setUnitPrice(product.getPrice());
            orderItem.setProductName(product.getName());
            orderItem.setDetails(product.getDetails());
            orderItemRepository.save(orderItem);
        }
        order.setTotalPrice(totalPrice);
        orderRepository.save(order);

        return true;
    }

    @Override
    public Map getOrders(Integer userId) {
        List<Order> orders = orderRepository.findByUserId(userId);
        Map<Integer, List<OrderItem>> ordersMap = new HashMap<>();
        for (Order order : orders) {
            Integer orderId = order.getId();
            ordersMap.put(orderId, orderItemRepository.findByOrderId(orderId));
        }

        return ordersMap;
    }

    @Override
    public Map getAllOrders() {
        List<Order> orders = orderRepository.findAll();
        Map<Integer, List<OrderItem>> ordersMap = new HashMap<>();
        for (Order order : orders) {
            Integer orderId = order.getId();
            ordersMap.put(orderId, orderItemRepository.findByOrderId(orderId));
        }
        return ordersMap;
    }
}
